// pages/demo/demo.js
// let City = require('../../utils/allcity.js');
let City = require('../../utils/newcity.js');

const app = getApp()
const { theagentlist } = require('../../utils/api')
const { fetch } = require('../../utils/fetch')

Page({

  data: {
    city: [],
    config: {
      horizontal: true, // 第一个选项是否横排显示（一般第一个数据选项为 热门城市，常用城市之类 ，开启看需求）
      animation: true, // 过渡动画是否开启
      search: true, // 是否开启搜索
      searchHeight: 45, // 搜索条高度
      suctionTop: true // 是否开启标题吸顶
    },
    currentAddress: null,
    selarea: app.globalData.selarea,
    agentList:[],
    specificarea:'',
    historyAreaList: []
  },
  onLoad() {
    // // 模拟服务器请求异步加载数据
    // setTimeout(()=>{
    /*app.setWatching('currentAddress', currentAddress => {
      this.setData({
        currentAddress
      })
    })*/

    if(wx.getStorageSync('arrcity')) {
      var arrcity = wx.getStorageSync('arrcity')
      console.log(arrcity, '收代理费')
			if(arrcity.length>3) {
        this.setData({
          historyAreaList:arrcity.slice(0,3)
        })
        wx.setStorageSync('arrcity', arrcity.slice(0,3))
			} else {
        this.setData({
          historyAreaList:arrcity.slice(0,3)
        })
        wx.setStorageSync('arrcity', arrcity.slice(0,3))
			}
		}

    if(wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea:wx.getStorageSync('specificarea')
      })
    }
    
    if (app.globalData.currentAddress) {
      this.setData({
        currentAddress: app.globalData.currentAddress
      })
    } else {
      app.LocaInfoReadyCallback = res => {
        this.setData({
          currentAddress: res
        })
      }
    }


    if(wx.getStorageSync('selarea')) {
      this.setData({
        selarea: wx.getStorageSync('selarea') 
      });
    } else {
        this.setData({
            selarea: app.globalData.selarea
          });
    }
    
    
    this.setData({
      city: City,
      // selarea: app.globalData.selarea
    })
    let userInfo = wx.getStorageSync('userInfo');
    fetch(`wx/areainfo/${this.data.specificarea}/agentlist`, 'POST', {  memberId: userInfo["id"] }).then(res => {
      if(res) {
        this.setData({
          agentList: res
        })
      }
    })

  },
  bindtap(e) {
    var data={
      district: e.detail.name,
      adcode:'333333'
    }
    app.set_sel_area(data);
    wx.redirectTo({
      url: '/pages/index/index?selarea='+data.district
    })
  },

})